<?php
/**
 * Created By Project
 * Author: 陈东
 * Copyright：陈东
 * Source Code：陈东
 * Explain：I shall not authorize for any other commercial purposes, law breakers must be prosecuted.
 */
namespace Admin\Widget;
use Think\Controller;
class MenutypeWidget extends Controller {

	//根据奖品ID统计奖品数量
	public function awardtype($rootid,$matypeid,$r,$lingqu=null){
		$randnumber = D(C('TABLE_RANDNUMBER'));
		$where['ma_rootid'] = $rootid;
		if($r==1){
			$where['ma_type'] = $r;
		}else{
			$where['ma_type'] = $matypeid;
		}
		if(!empty($lingqu)){
			$where['ma_hb_cishu'] = 1;
		}
		$where['isvalid'] = array('neq',0);
		$res = $randnumber->where($where)->count();
		echo $res;
	}
	/*
	 * 根据用户ID获取用户名称
	 */
	public function showuser($userid=null){
	
		if(!empty($userid)){
			$user = D(C('TABLE_USER'));
			$userresult = $user->find($userid);
			return $userresult['username'];
		}
	
	}
	/*
	 * 显示资源类型
	 */
	public function showtype(){
		$str = null;
		$arr = array('1','2','3','4');
		foreach ($arr as $v){
			$str .= "<option value=".$v.">".menutype($v)."</option>";
		}
		return $str;
	}
	
	/*
	 * 获取选择的模板
	 */
	public function gettpl($tpl){
		switch ($tpl){
			case 'index2':
				echo "模板2";
				break;
			case 'index3':
				echo "模板3";
				break;
			case 'index4':
				echo "模板4";
				break;
			case 'index5':
				echo "模板5";
				break;
			case 'index6':
				echo "模板6";
				break;
			case 'index7':
				echo "模板7";
				break;	
			case 'index8':
				echo "模板8";
				break;
			case 'index9':
				echo "模板9";
				break;
			case 'index10':
				echo "模板10";
				break;
			default:
				echo "模板1";
		}
	}
	
	/*
	 * 显示资源分类
	 */
	public function classtype(){
		$str = null;
		$arr = array('1','2','3','4','5','6');
		foreach ($arr as $v){
			$str .= "<option value=".$v.">".classtypename($v)."</option>";
		}
		return $str;
	}
    /*
     * 菜单列表(递归循环输出)
     * $len 递归层数
     * $id 菜单ID
     */
	public function showmenuinfo($id,$len=0){
		$str = null;
		$submenu = D(C('TABLE_SUBMENU'));
		if($len>0){
			$result = $submenu->where(array('mparent'=>$id))->select();
		}else{
			$result = $submenu->where("mtypeid=%d and mparent=%d",array($id,0))->order('msort asc')->select();
		}
		foreach($result as $v){
			if($v['mparent']==0){
				if(!empty($v['binid'])){
					$lzi = "<div style='margin-top: 6px;'>来自：".$this->gettypema($v['binid'])."<a class='cancelgl' href='".U('cancelgl',array('id' => $v['id']))."'>取消关联</a></div>";
				}else{
					$lzi = "";
				}
				/*1（企业） 2（政府）*/
				$p = gettypeaddres($v['classtype'],$v['id'],$v['mtypeid']);
				$preview = "<a class='preview' path='".$p."'>预览</a>";
				if(!empty($lzi)){
					$glian = $lzi;
				}else{
					$glian = "<span style=\"position:inherit; width:20%;\" v=\"".$v['id']."\">关联</span>".$lzi;
				}
				
			}
			if(session('userinfo')['type']==1){
				$c_str = " / <a href='".U('menuedit',array('id'=>$v['id'],'rootid'=>I('get.id')))."'>编辑</a>";
				$c_str .= " / <a onclick=if(confirm('".C('DELETEMESSAGE')."')){submenudelete(\"".U('menudelete',array('id'=>$v['id']))."\")} style='cursor:pointer'>删除</a>";
			}
			//$deletestr = " / <a onclick=if(confirm('".C('DELETEMESSAGE')."')){submenudelete(\"".U('menudelete',array('id'=>$v['id']))."\")} style='cursor:pointer'>删除</a>";
			$str .= "<tr class='tr-line'>";
			$str .= "<td>".$v['id']."</td>";
			$str .= "<td><div class='menutitle'><a href='".U('articlelist',array('id'=>$v['id'],'rootid'=>I('get.id')))."'>".str_repeat('——',$len).$v['mtitle']."</a></div><div class=\"postionshow\">".postionr($v['position'])."</div></td>";
			$str .= "<td class=\"guanlian\">".$glian."</td>";
			$str .= "<td>".$v['malias']."</td>";
			$str .= "<td>".menutype($v['mtype'])."</td>";
			$str .= "<td style='font-size:12px;'>".$v['mcrdate']."</td>";
			//$str .= "<td><a onclick=changestate('".U('Type/changestate',array('id'=>$v['id'],'state'=>$v['state'],'table'=>'submenu'))."')>".chagestateshow($v['state'])."</a></td>";
			$str .= "<td>".$v['msort']."</td>";
			$str .= "<td style='text-align:right;'>".$preview.$c_str."</td>";
			$str .= "</tr>".$this->showmenuinfo($v['id'],$len+1);
		}
		return $str;
	}
	
	protected function gettypema($id){
		$type = D(C('TABLE_TYPE'));
		$res = $type->find($id);
		if(!empty($res)){
			$getthisname = $res['typename'];
		}
		if(!empty($getthisname)){
			return $getthisname;
		}else{
			return $this->gettypema2($this->gettypema1($id));
		}
	}
	
	protected function gettypema2($id){
		$type = D(C('TABLE_TYPE'));
		$res = $type->find($id);
		if(!empty($res)){
			return $res['typename'];
		}
	}
	
	protected function gettypema1($id){
		$stype = D(C('TABLE_SUBMENU'));
		$res1 = $stype->find($id);
		return $res1['mtypeid'];
	}
    /*
     * 获取菜单名称
     */
    public function getmenuname($id=null){
    	if(isset($id)){
    		$submenu = D(C('TABLE_SUBMENU'));
    		$result = $submenu->where(array('id'=>$id))->find();
    		return $result['mtitle'];
    	}
    }
    /**
     * 面包屑
     */
    public function nowhere($id = null){
    	if($id > 0){
			$submenu = M(C('TABLE_SUBMENU'));
    		$result  = $submenu->field('id,mtitle,mparent,mtypeid')->where(array('id' => $id))->find();
			if($result){
				$mtitle  = $result['mtitle'];
				$mparent = $result['mparent'];
				if($mparent > 0){
					$str .= $this->nowhere($mparent)." > <a href=".U('Index/articlelist',array('id' => $result['id'], 'rootid' => $result['mtypeid'])).">". $mtitle ."</a>";
				}else{
					$str = "<a href=".U('Index/articlelist',array('id' => $result['id'])).">". $mtitle ."</a>";
				}
			}
    	}
		return $str;
    }
    
    public function typename($mtypeid){
    	$type = D(C('TABLE_TYPE'));
    	$typeresult = $type->field('id,typename')->find($mtypeid);
    	return "<a style=\"color: #606060;\" href=\"/Admin/Index/menulist/id/".$typeresult['id']."\">".$typeresult['typename']."</a>";
    }
    /**
     * 根据rootid获取所有子菜单ID号
     */
    protected function getallid($submenu, $id = null){
    	if(!empty($id)){
    		$n = $id.",";
    		$where['mparent'] = array('in',$id);
    		$result = $submenu->field('id,mparent')->where($where)->select();
    		if(!empty($result)){
    			foreach ($result as $v){
    				$n .= $v['id'].",".$this->getallid($submenu, $v['id']);
    				//$n .= $v['id'].",";
    			}
    		}
    		return $n;
    	}
    }
    /*
     * 资源类型
     */
    public function resourcetype($name,$typename,$v=null){
    	$str = null;
    	$str .= "<select name='".$name."' class='template form-control'>";
    	$str .= "<option value=''>请选择</option>";
    	foreach ($typename as $k=>$tname){
    		$k = $k+1;
    		$selected = null;
    		if(!empty($v)){
    			if($k==$v){
    				$selected = " selected";
    			}
    		}
    		$str .= "<option value='".$k."'".$selected.">".$tname."</option>";
    	}
    	$str .= "</select>";
    	return $str;
    }
        
    /*
     * 所属基金会
     */
    public function pjjh($id){
    	if(!empty($id)){
    		$d = D(C("TABLE_JIJINHUI"));
    		$res = $d->find($id);
    		echo $res['j_name'];
    	}
    	
    }
    
    /*
     * 资源列表下属分类
     */
    public function subresource($id){
    	$resource = D(C('TABLE_RESOURCE'));
    	$res = $resource->where(array('pid'=>$id))->select();
    	if($res){
    		return $res;
    	}else{
    		return false;
    	}
    }
    
    /*
     * 获取所属资源栏目
     */
    public function getresourcename($id){
    	if(!empty($id)){
    		$resource = D(C('TABLE_RESOURCE'));
    		$res = $resource->find($id);
    		if($res){
    			$tname = $res['typename'];
    			if($res['pid']==0){
    				$tnames = $tname;
    			}else{
    				$tnames = $this->getresourcename($res['pid']).' -> '.$tname;
    			}  			
    		}else{
    			$tnames = '栏目已删除，请重新归类';
    		}
    		echo $tnames;
    	}
    }

	/*
     * 获取所属板块
    */
	public function getplatename($id){
		if(!empty($id)){
			$resource = D(C('TABLE_PLATE'));
			$res = $resource->find($id);
			if($res){
				echo $res['typename'];
			}else{
				echo '栏目已删除，请重新归类';
			}
		}
	}

	/*
	 * 自助华人码，根据token获取用户联系方式
	 */
	public function contactway($token){
		$selfhelp = D(C('TABLE_SELFHELP'));
		$res = $selfhelp->where(array('user_token'=>$token))->find();
		if($res){
			return $res['user_tel'];
		}else{
			return '-';
		}
	}
    //获取什么奖品
	public function matype($matype,$afield='awardname'){
		$award = D(C('TABLE_AWARD'));
		if($matype!=1){
			$res = $award->find($matype);
			if($res){
				echo $res[$afield];
			}
		}else{
			echo '钻石';
		}
	}
	//检测图片是否有效
	public function checkimg($imgpath){
		$arr = getimagesize($imgpath);
		if($arr){
			echo "<a href='".$imgpath."' target='_blank'><img src='".$imgpath."' height=50 /></a>";
		}else{
			echo '-';
		}
	}
	//根据评论的ID获取文章标题
	public function getarticleinfo($id,$field){
		$article = D(C('TABLE_ARTICLE'));
		$res = $article->find($id);
		echo $res[$field];
	}

	/**
	 * @param $str
	 * 显示扩展属性
	 */
	public function getexten($img, $str){
		$res = null;
		if(!empty($img)) $res .= "<b class='extena'>[图]</b>";
		$str = explode(",", $str);
		foreach($str as $v){
			if(!empty($v)){
				$res .= $this->show_extenname($v);
			}
		}
		echo $res;
	}

	protected function show_extenname($str){
		$set_attr = $this->set_attr();
		foreach($set_attr as $v){
			if($v['exten'] == $str){
				return "<b class='extena'>[".$v['exten_name']."]</b>";
			}
		}
	}

	protected function set_attr(){
		return  array(
			array(
				'exten' => 't',
				'exten_name' => '特'
			),
			array(
				'exten' => 'c',
				'exten_name' => '推'
			),
			array(
				'exten' => 'j',
				'exten_name' => '跳'
			),
			array(
				'exten' => 'e',
				'exten_name' => '独'
			),
			array(
				'exten' => 'h',
				'exten_name' => '热'
			),
			array(
				'exten' => 'f',
				'exten_name' => '焦'
			),
			array(
				'exten' => 'l',
				'exten_name' => '左'
			),
			array(
				'exten' => 'r',
				'exten_name' => '右'
			),
			array(
				'exten' => 'u',
				'exten_name' => '上'
			),
			array(
				'exten' => 'd',
				'exten_name' => '下'
			)
		);
	}
    
    
}
?>